package com.zb.kc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zb.kc.commonutils.R;
import com.zb.kc.entity.EduSubject;
import com.zb.kc.service.IEduSubjectService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author wanggen
 * @since 2020-04-09
 */
@RestController
@RequestMapping("/eduservice/subject")
public class EduSubjectController {

    @Autowired
    private IEduSubjectService subjectService;

    /**
     * 添加课程分类
     *
     * @param file
     * @return
     */
    @PostMapping
    @ApiOperation("添加课程分类")
    public R addSubject(MultipartFile file) {
        // 保存获取来的文件
        subjectService.saveSubject(file, subjectService);
        return R.ok();
    }

    /**
     * 获取所有的课程分类，树形结构显示
     *
     * @return
     */
    @GetMapping
    @ApiOperation("树形结构的课程分类")
    public R getAllSubject() {
        List<EduSubject> allSubjectTree = subjectService.getAllSubjectTree();
        return R.ok().data("subs", allSubjectTree);
    }

    /**
     * 根据父id查
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R getSubjectById(@PathVariable("id") String id) {
        List<EduSubject> subjects = subjectService.list(new QueryWrapper<EduSubject>().eq("parent_id", id));
        return R.ok().data("subjects", subjects);
    }

    /**
     * 根据父id查
     * @param id
     * @return
     */
    @GetMapping("getAll2")
    public R getAll2(String id) {
        List<EduSubject> subjects = subjectService.list2(id);
        return R.ok().data("subjects", subjects);
    }


}
